<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>While-loop</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>While-loop <span class="ver">[v1.0.48+]</span></h1>

<p>Performs a series of commands repeatedly until the specified <a href="../Variables.htm#Expressions">expression</a> evaluates to false.</p>

<pre class="Syntax">While <i>Expression</i></pre>
<h3>Parameters</h3>
<dl>

  <dt>Expression</dt>
  <dd><p>Any valid <a href="../Variables.htm#Expressions">expression</a>. For example: <code>while x &lt; y</code>.</p></dd>

</dl>

<h3>Remarks</h3>
<p>The expression is evaluated once before each iteration. If the expression evaluates to true (which is any result other than an empty string or the number 0), the body of the loop is executed; otherwise, execution jumps to the line following the loop's body.</p>
<p>A while-loop is usually followed by a <a href="Block.htm">block</a>, which is a collection of statements that form the <em>body</em> of the loop. However, a loop with only a single statement does not require a block (an &quot;if&quot; and its &quot;else&quot; count as a single statement for this purpose).</p>
<p><a name="otb"></a>The One True Brace (OTB) style may optionally be used, which allows the open-brace to appear on the same line rather than underneath. For example: <code>while x &lt; y {</code>.</p>
<p>The built-in variable <strong>A_Index</strong> contains the number of the current loop iteration. It contains 1 the first time the loop's expression and body are executed. For the second time, it contains 2; and so on. If an inner loop is enclosed by an outer loop, the inner loop takes precedence. A_Index works inside all types of loops, but contains 0 outside of a loop.</p>
<p>As with all loops, <a href="Break.htm">Break</a> may be used to exit the loop prematurely. Also, <a href="Continue.htm">Continue</a> may be used to skip the rest of the current iteration, at which time A_Index is increased by 1 and the while-loop's expression is re-evaluated. If it is still true, a new iteration begins; otherwise, the loop ends.</p>
<p>Specialized loops: Loops can be used to automatically retrieve files, folders, or registry items (one at a time). See <a href="LoopFile.htm">file-loop</a> and <a href="LoopReg.htm">registry-loop</a> for details. In addition, <a href="LoopReadFile.htm">file-reading loops</a> can operate on the entire contents of a file, one line at a time. Finally, <a href="LoopParse.htm">parsing loops</a> can operate on the individual fields contained inside a delimited string.</p>
<h3>Related</h3>
<p><a href="Until.htm">Until</a>, <a href="Break.htm">Break</a>, <a href="Continue.htm">Continue</a>, <a href="Block.htm">Blocks</a>, <a href="Loop.htm">Loop</a>, <a href="For.htm">For-loop</a>, <a href="LoopFile.htm">Files-and-folders loop</a>, <a href="LoopReg.htm">Registry loop</a>, <a href="LoopReadFile.htm">File-reading loop</a>, <a href="LoopParse.htm">Parsing loop</a>, <a href="IfExpression.htm">If (expression)</a></p>
<h3>Examples</h3>
<pre class="NoIndent"><em>; As the user drags the left mouse button, a ToolTip displays the size of the region inside the drag-area.</em>

CoordMode, Mouse, Screen

~LButton::
    MouseGetPos, begin_x, begin_y
    <strong>while</strong> GetKeyState(&quot;LButton&quot;)
    {
        MouseGetPos, x, y
        ToolTip, % begin_x &quot;, &quot; begin_y &quot;`n&quot; Abs(begin_x-x) &quot; x &quot; Abs(begin_y-y)
        Sleep, 10
    }
    ToolTip
return</pre>

</body>
</html>
